#include "ChoseGateScene.h"
#include "ChoseGoodsScene.h"
#include "WelcomeScene.h"
#include "cocostudio/CocoStudio.h"
#include "AppMacros.h"

bool ChoseGateScene::init() {
	if (!Layer::init()) {
		return false;
	}
	
	auto node = CSLoader::createNode("ChoseGateScene.csb");
	node->setPosition(Vec2::ZERO);
	this->addChild(node);
	
	dens = new Vector<Button*>;
	for (int i = 1; i <= GATE_COUNT; i++) {
		Button* btn =dynamic_cast<Button*>( node->getChildByName("ScrollView_1")->getChildByTag(i));
		if (btn == nullptr) {
			log("The Button is Null!");
		}
		else {
			dens->pushBack(btn);
			btn->addClickEventListener([i](Ref* ref) {
				log("%d", i);
				Director::getInstance()->replaceScene(TransitionFlipX::create(0.3f,ChoseGoodsScene::createScene()));
			});
		}
	}
	auto back_btn =dynamic_cast<Button*>( node->getChildByName("back_btn"));
	if (back_btn == nullptr) {
		log("back button is null!");
	}
	else {
		back_btn->addClickEventListener([](Ref* ref) {
			Director::getInstance()->replaceScene(TransitionFadeDown::create(0.3f,WelcomeScene::createScene()));
		});
	}

	return true;
}

Scene* ChoseGateScene::createScene() {
	auto scene = Scene::create();
	auto layer = ChoseGateScene::create();
	scene->addChild(layer);
	return scene;
}